package com.abc.xxx.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@TableName("t_view")
public class View {

    /**
     * 如果某种类型的权限是树状的，且如果这种权限的某个元素位于树结构的顶部，它的父级代码就是ROOT_ELEMENT_PARENT_CODE
     */
    public static final String ROOT_ELEMENT_PARENT_CODE = "0";

    /**
     * 权限类型：页面
     */
    public static final Integer PAGE = 1;

    /**
     * 权限类型：按钮
     */
    public static final Integer BUTTON = 2;

    @TableId(type = IdType.INPUT)
    private String code;
    private String name;
    private Integer type;
    private String tag;
    @TableField("`order`")
    private Integer order;
    private String parent;
    private String parents;

    @TableField(exist = false)
    private List<View> children = new ArrayList<>();

    public boolean isRootElement() {
        return ROOT_ELEMENT_PARENT_CODE.equals(this.parent);
    }

    public boolean isPage() {
        return PAGE.equals(this.type);
    }

    public boolean isButton() {
        return BUTTON.equals(this.type);
    }

    public boolean isChildOf(String parentCode) {
        return this.parent != null && this.parent.equals(parentCode);
    }

}
